<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';
echo $action;
switch ($action) {
	
	case 'addBook' :
		addBook();
		break;
		
	case 'modifyBook' :
		modifyBook();
		break;
		
	case 'deleteBook' :
		deleteBook();
		break;   
	
	case 'deleteImage' :
		deleteImage();
		break;
		
	default :
	    // if action is not defined or unknown
		// move to main Book page
		header('Location: index.php');
}


function addBook()
{
    $nganh_id  	= (int)$_POST['cboBranch'];
	$id   		= (int)$_GET['id'];
    $masach      = $_POST['txtMasach'];
	$tensach 	= $_POST['txtTensach'];
	$namxb = $_POST['txtNamxb'];
	$nhaxb = $_POST['txtNhaxb'];
	
	$images = uploadBookImage('fleImage', SRV_ROOT . 'images/book/');

	$hinhanh = $images['image'];
	$hinhanh_thumb = $images['thumbnail'];
	
	$sotrang 	= $_POST['txtSotrang'];
	$kichthuoc 	= $_POST['txtKichthuoc'];
	$trongluong	= $_POST['txtTrongluong'];
	$soluong	= $_POST['txtSoluong'];
	$solantaiban	= $_POST['txtSolantaiban'];
	
	$sql2   = "INSERT INTO sach(masach, tensach, namxb, nhaxb, hinhanh, hinhanh_thumb, sotrang, kichthuoc, trongluong, soluong, solantaiban, nganh_id)
	          VALUES ('$masach', '$tensach', $namxb, '$nhaxb', '$hinhanh', '$hinhanh_thumb', $sotrang, '$kichthuoc', $trongluong, $soluong, $solantaiban, $nganh_id)";

	$result2 = dbQuery($sql2);

	header("Location: index.php");	
}

function uploadBookImage($inputName, $uploadDir)
{
	$image     = $_FILES[$inputName];
	$imagePath = '';
	//$thumbnailPath = '';
	
	// if a file is given
	if (trim($image['tmp_name']) != '') {
		$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

		// generate a random new file name to avoid name conflict
		$imagePath = md5(rand() * time()) . ".$ext";
		
		list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

		// make sure the image width does not exceed the
		// maximum allowed width
		if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
			$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
			$imagePath = $result;
		} else {
			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
		}	
		
		if ($result) {
			// create thumbnail
			$thumbnailPath =  md5(rand() * time()) . ".$ext";
			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
			
			// create thumbnail failed, delete the image
			if (!$result) {
				unlink($uploadDir . $imagePath);
				$imagePath = $thumbnailPath = '';
			} else {
				$thumbnailPath = $result;
			}	
		} else {
			// the Book cannot be upload / resized
			$imagePath = $thumbnailPath = '';
		}
		
	}

	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}


//	Modify a Book

function modifyBook()
{
	
	$nganh_id  	= (int)$_POST['cboBranch'];
	$id   		= (int)$_GET['id'];
    $masach      = $_POST['txtMasach'];
	$tensach 	= $_POST['txtTensach'];
	$namxb = $_POST['txtNamxb'];
	$nhaxb = $_POST['txtNhaxb'];
	
	$images = uploadBookImage('fleImage', SRV_ROOT . 'images/book/');

	$mainImage = $images['image'];
	$thumbnail = $images['thumbnail'];

	// if uploading a new image
	// remove old image
	if ($mainImage != '') {
		_deleteImage($id);
		
		$mainImage = "'$mainImage'";
		$thumbnail = "'$thumbnail'";
	} else {
		// if we're not updating the image
		// make sure the old path remain the same
		// in the database
		$mainImage = 'hinhanh';
		$thumbnail = 'hinhanh_thumb';
	}
	$sotrang 	= $_POST['txtSotrang'];
	$kichthuoc 	= $_POST['txtKichthuoc'];
	$trongluong	= $_POST['txtTrongluong'];
	$soluong	= $_POST['txtSoluong'];
	$solantaiban	= $_POST['txtSolantaiban'];

	$sql2   = " UPDATE sach
               SET tensach = '$tensach', namxb = $namxb, nhaxb = '$nhaxb', hinhanh = $mainImage, hinhanh_thumb = $thumbnail, sotrang = $sotrang, kichthuoc = '$kichthuoc', trongluong = $trongluong, soluong = $soluong, solantaiban = $solantaiban, nganh_id = $nganh_id
			   WHERE id = $id " ;		
	//echo $sql2; exit();
	dbQuery($sql2);
	
	header('Location: index.php');			  
}


/*
	Remove a Book image
*/
function deleteImage()
{
	if (isset($_GET['id']) && (int)$_GET['id'] > 0) {
		$id = (int)$_GET['id'];
	} else {
		header('Location: index.php');
	}
	
	$deleted = _deleteImage($id);

	// update the image and thumbnail name in the database
	$sql = "UPDATE sach
			SET hinhanh = '', hinhanh_thumb = ''
			WHERE id = $id";
	dbQuery($sql);		

	header("Location: index.php?view=modify&id=$id");
}
function _deleteImage($id)
{
	// we will return the status
	// whether the image deleted successfully
	$deleted = false;
	
	$sql = "SELECT hinhanh, hinhanh_thumb 
	        FROM sach
			WHERE id = $id";
	$result = dbQuery($sql) or die('Cannot delete book image. ' . mysql_error());
	
	if (dbNumRows($result)) {
		$row = dbFetchAssoc($result);
		extract($row);
		
		if ($hinhanh && $hinhanh_thumb) {
			// remove the image file
			$deleted = @unlink(SRV_ROOT . "images/book/$hinhanh");
			$deleted = @unlink(SRV_ROOT . "images/book/$hinhanh_thumb");
		}
	}
	
	return $deleted;
}
//Delete Product
function deleteBook()
{
	if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
	} else {
		header('Location:index.php');
	}	
		
	$sql1 = "UPDATE sach SET hieu_luc = 0 WHERE id = $id"; 
	//$sql = "delete from nhomnc where masach = '$masach'";

	dbQuery($sql1);
		
    header('Location: index.php'); 
}

?>